<?php

/**
 * @author hungnq@younetco.com
 */
class Core_Model_DbTable_Pages extends YouNet_Db_Table
{
	const TABLE_NAME = 'contest_pages';
	protected $_primary = array('id_page', 'id_contest');
	protected $_name = self::TABLE_NAME;
	protected $_rowClass = 'Core_Model_Page';
	private static $_instance;
	protected $_referenceMap = array(
		'Contest' => array(
			'columns' => 'id_contest',
			'refTableClass' => 'Core_Model_DbTable_Contests',
			'refColumns' => 'id'
		),
	);

	/**
	 * @author hungnq
	 * @return Core_Model_DbTable_Pages
	 */
	public static function getInstance()
	{
		if(is_null(self::$_instance))
		{
			self::$_instance = new self();
		}
		return self::$_instance;
	}

	/**
	 * Select the most current contest by id page.
	 * @see Core_Model_Db_Table::findContestByPage
	 * @param varchar(20) $id_page 
	 */
	public function findContestByPage($id_page)
	{
		$query = $this->select()
				->setIntegrityCheck(false)
				->from($this)
				->joinLeft(array('ct' => Core_Model_DbTable_Contests::TABLE_NAME), 'ct.id = id_contest', array('ct.status'))
				->where('id_page=?', $id_page)
				->where('status=?', Core_Model_IContest::STATUS_PUBLISHED)
				->limit(1);

		return $this->fetchAll($query)->current();
	}

	public function findIdContestByPage($id_page)
	{
		$result = $this->findContestByPage($id_page);
		if(!empty($result))
		{
			return $result->id_contest;
		}
		return null;
	}
	
	public function getFanpageByIdContest($id_contest)
	{
		$query = $this->select()
				->from($this,array('id_page','count(id_page) as number_of_page'))
				->where('id_contest=?',$id_contest)
				->limit(1);
		return $this->fetchAll($query)->current();
	}
    
	public static function getOtherPublishedPageIds($id_form, array $arrPageIds)
	{
        if(!empty($arrPageIds)){
            $select = self::getInstance()->select()
                ->from(array(
                        'p' => self::TABLE_NAME				
                    ),
                    array('p.id_page')
                )
                ->setIntegrityCheck(false)
                ->join(
                    array('f' => Core_Model_DbTable_Contests::TABLE_NAME),
                    'f.id = p.id_contest',
                    array()
                )
                ->where('f.id <> ?', $id_form)
                ->where('f.status = ?', Core_Model_IContest::STATUS_PUBLISHED)
                ->where('p.id_page IN (?)', $arrPageIds);
            return self::$_defaultDb->fetchCol($select);
        } else {
            return null;
        }
	}
	/**
	 * @author hungnq@younetco.com
	 * @param type $id_contest
	 * @return array
	 */
	public function getArrayPagesID($id_contest)
	{
		$pages		= array();
		$pageRowSet = $this->_getPageRowSet($id_contest);
		if($pageRowSet->count())
		{
			foreach($pageRowSet as $row)
			{
				$pages[] = $row->id_page;
			}
		}
		return $pages;
	}
	
	/**
	 *
	 * @param type $id_contest
	 * @return RowSet 
	 */
	public function _getPageRowSet($id_contest)
	{
		$query = $this->select()->where('id_contest=?',$id_contest);
		return $this->fetchAll($query);
	}
    	public function loadCacheIdFormByIdPage($id_page)
	{
		$key		= Core_Cache_IKeys::MEMCACHE_CONTEST_ID_PAGES.$id_page;
		return $this->_memcacheLoad($key);
	}

	public function saveCacheIdFormWithIdPage($id_form,$id_page)
	{
		$key		= Core_Cache_IKeys::MEMCACHE_CONTEST_ID_PAGES.$id_page;
		return $this->_memcacheSave($id_form, $key);
	}

	public function removeCacheIdFormByIdPage($id_page)
	{
		$key		= Core_Cache_IKeys::MEMCACHE_CONTEST_ID_PAGES.$id_page;
		return $this->_memcacheRemove($key);
	}
    protected function _memcacheLoad($key)
	{
		return YouNet_Util::getObjectMemCache()->load($key);
	}
	protected function _memcacheRemove($key)
	{
		return YouNet_Util::getObjectMemCache()->remove($key);
	}
	protected function _memcacheSave($data, $key)
	{
		return YouNet_Util::getObjectMemCache()->save($data, $key);
	}
    public   function getNamePage($id_page){
        $query = $this->select()
				->from($this,array('name_page'))
				->where('id_page=?',$id_page)
				->limit(1);
         return $this->getAdapter()->fetchOne($query);
    }
}
